package com.twotiger.and.activity.base;

import java.util.HashMap;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.twotiger.and.Constants;
import com.twotiger.and.util.HttpUtil;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.NetWorkUtil;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.RandomUtils;
import com.twotiger.and.util.Signature;

/**
 * 业务
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月17日 下午7:05:35
 */
public class BaseService {

    protected Context mContext;

    public BaseService(Context mContext) {
        this.mContext = mContext;
    }

    public void getDate(HashMap<String, String> params, String path, final Handler handler, final int TUREMESSAGER, final int ERRORMESSAGER, final boolean showLoading) {
        if (!NetWorkUtil.isNetworkConnected(mContext)) {
            PromptManager.showLoadDataDialog(mContext, "网络不可用，请检查您的网络连接");
            return;
        }
        params.put("version", "2");
        String randomNumber = RandomUtils.getRandomNumbersAndLetters(32);
        String sign = Signature.getSign(params, Constants.Appkey, randomNumber);
        params.put("nonceStr", randomNumber);
        params.put("sign", sign);
        LogUtil.info(BaseActivity.class, path + "?" + params.toString());
        HttpUtil.doGet(mContext, path, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (showLoading) {
                    PromptManager.showLoadDataDialog(mContext, "正在加载...");
                }
                super.onStart();
            }

            @Override
            public void onSuccess(ResponseInfo<String> result) {
                PromptManager.closeLoadDataDialog();
                LogUtil.info("请求成功" + result.result);
                Message message = Message.obtain();
                message.what = TUREMESSAGER;
                message.obj = result.result;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(HttpException e, String result) {
                PromptManager.closeLoadDataDialog();
                LogUtil.info("请求失败：" + result);
                Message message = Message.obtain();
                message.what = ERRORMESSAGER;
                message.obj = result;
                handler.sendMessage(message);
            }
        });

    }
}
